package com.atguigu01.collection;

import javax.swing.*;
import java.awt.*;

public class Demo20 extends JPanel {
    private int centerX, centerY;
    private int radiusDiff = 40; // 半径差值
    private int circleCount = 5; // 同心圆个数
    private int maxRadius = 200; // 最大同心圆半径
    private int sectorCount = 12; // 扇形个数
    private int lineWidth = 3; // 分割线宽度

    public Demo20() {
        setPreferredSize(new Dimension(400, 400));
        setBackground(Color.WHITE); // 设置背景为白色
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        centerX = getWidth() / 2;
        centerY = getHeight() / 2;

        // 绘制扇形
        g2d.setColor(Color.WHITE); // 设置扇形颜色为白色
        for (int i = 0; i < sectorCount; i++) {
            int startAngle = i * 360 / sectorCount;
            int endAngle = (i + 1) * 360 / sectorCount - startAngle;
            g2d.fillArc(centerX - maxRadius, centerY - maxRadius, 2 * maxRadius, 2 * maxRadius, startAngle, endAngle);
            g2d.setColor(Color.BLACK);
            g2d.setStroke(new BasicStroke(lineWidth));
            g2d.drawArc(centerX - maxRadius, centerY - maxRadius, 2 * maxRadius, 2 * maxRadius, startAngle, endAngle);
            g2d.setColor(Color.WHITE);
            g2d.setStroke(new BasicStroke(1));
            int x1 = centerX;
            int y1 = centerY;
            int x2 = (int) (centerX + maxRadius * Math.cos(startAngle * Math.PI / 180));
            int y2 = (int) (centerY + maxRadius * Math.sin(startAngle * Math.PI / 180));
            g2d.setColor(Color.BLACK); // 将画笔颜色设置为黑色
            g2d.drawLine(x1, y1, x2, y2);
        }

        // 绘制同心圆
        g2d.setColor(Color.BLACK); // 设置同心圆线条颜色为黑色
        int radius = maxRadius;
        for (int i = 0; i < circleCount; i++) {
            radius = radius - radiusDiff;
            int x = centerX - radius;
            int y = centerY - radius;
            g2d.drawOval(x, y, 2 * radius, 2 * radius);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Concentric Circles");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new Demo20());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}